import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { RoleEnum } from 'src/role/role.enum';
import { UserGuard } from 'src/role/role.guard';
import { LogService } from './log.service';
import { FindOptionsWhere, In } from 'typeorm';
import { Log } from './log.entity';

@Controller('api/log')
@ApiTags("日志")
@UseGuards(AuthGuard("jwt"), UserGuard([RoleEnum.director]))
export class LogController {
    constructor(
        private readonly logService: LogService
    ) { }

    @Get("get")
    @ApiOperation({ summary: "查看日志" })
    async get(
        @Query("limit") limit: number,
        @Query("page") page: number,
        @Query("user") user: number
    ) {
        if (!page) page = 1;
        if (!limit) limit = 10
        const where: FindOptionsWhere<Log> = {};
        if (!!user) where.user = { id: user }
        const [data, count] = await this.logService.log.findAndCount({
            order: {
                id: "desc"
            },
            take: limit,
            skip: (page - 1) * limit,
            where,
            relations: ["user"]
        })
        return { data, count }
    }

    @Post("del")
    @ApiOperation({ summary: "删除日志" })
    del(
        @Body() { ids }
    ) {
        return this.logService.log.delete({
            id: In(ids)
        })
    }
    @Get("delall")
    @ApiOperation({ summary: "删除全部" })
    delall() {
        return this.logService.log.delete({})
    }
}
